"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateIconfont = void 0;
const templates_1 = require("./templates");
const tools_1 = require("./tools");
const fs = require("fs");
const path = require("path");
function generateIconfont(options) {
    options.outputValue = options.outputValue || "/dist";
    const { isPure, inputValue, outputValue, isDeep, id, isClazz } = options;
    const inputPathList = inputValue?.split("/").filter(Boolean) || [];
    const outputPathList = outputValue.split("/").filter(Boolean);
    const fileName = inputPathList[inputPathList.length - 1];
    const svgId = id || fileName;
    const inputPath = path.join(process.cwd(), ...inputPathList);
    const outputPath = path.join(process.cwd(), ...outputPathList);
    let symbolsList = [];
    if (fs.existsSync(inputPath) && fs.statSync(inputPath).isDirectory()) {
        const svgs = (0, tools_1.readSvgFiles)(path.join(inputPath), {
            isPure,
            isDeep,
        });
        symbolsList = [...symbolsList, ...svgs];
        outputIconfonts({ svgId, outputPath, isClazz, symbolsList });
    }
    else {
        throw new Error("input is not a directory");
    }
    // 输出文件
}
exports.generateIconfont = generateIconfont;
// 输出文件
async function outputIconfonts(options) {
    const { outputPath, isClazz, symbolsList, svgId } = options;
    const iconfontContent = (0, templates_1.generateIconfontContent)(symbolsList.map(templates_1.generateSymbolContnt).join(""), svgId);
    const idList = symbolsList.map((item) => item.name) || [];
    // 1 输出iconfont.js文件
    (0, tools_1.checkAndMakeDirPath)(outputPath);
    fs.writeFileSync(path.join(outputPath, `iconfont.js`), iconfontContent);
    // 2 输出React组件
    const iconPath = path.join(outputPath, "Icon");
    if (!fs.existsSync(iconPath) || !fs.statSync(iconPath).isDirectory()) {
        fs.mkdirSync(iconPath);
    }
    // 2.1 输出.tsx文件
    const tsxContent = isClazz
        ? (0, templates_1.generateClazzTsxContent)(idList)
        : (0, templates_1.generateFunTsxContent)(idList);
    fs.writeFileSync(path.join(outputPath, "Icon", "index.tsx"), tsxContent);
    // 2.2 输出.less文件
    const lessContent = (0, templates_1.generateLessContent)();
    fs.writeFileSync(path.join(outputPath, "Icon", "index.less"), lessContent);
    // 3 输出index.html文件，可以看到图标，点击图标复制type
    const htmlContent = (0, templates_1.generateHtmlContent)(idList);
    fs.writeFileSync(path.join(outputPath, "index.html"), htmlContent);
}
